# 单个商品信息
# product_info = {
#     "product": product_name,
#     "quantity": quantity,
#     "price": price
# }
import copy


# 添加商品
def add_soled_data():
  name = input("商品名称：")
  quantity = int(input("商品数量："))
  price = float(input("单价："))
  product_info = {
    "name": name,
    "quantity": quantity,
    "price": price
  }
  goods_list.append(product_info)


# 更新销售记录
def update_soled_data():
  good_name = input("输入需要修改销售记录的产品名：")
  for g in goods_list:
    if good_name == g.get("name"):
      quantity = int(input("商品数量："))
      price = float(input("单价："))
      new_g = {
        "name": good_name,
        "quantity": quantity,
        "price": price
      }
      goods_list.remove(g)
      goods_list.append(new_g)
      break
    else:
      continue
  return


# 删除销售记录
def delete_sold_data():
  good_name = input("输入需要删除销售记录的产品名：")
  for g in goods_list:
    if good_name == g.get("name"):
      goods_list.remove(g)
      print("已删除销售记录")
      break
    else:
      continue
  return


# 统计销售数据
def statistic_sold_sum():
  sum = 0
  for g in goods_list:
    sum += (g.get("price") * g.get("quantity"))
  print(f"总销售额为：{sum}元")
  return


# 备份销售数据
def back_up_sold_data(t, goods_list):
  backup = None
  if t == "deep":
    backup = copy.deepcopy(goods_list)
    print("已完成深拷贝！")
  elif t == "shallow":
    backup = copy.copy(goods_list)
    print("已完成浅拷贝")
  else:
    print("不存在的备份方式")
    return
  # 返回备份记录
  return backup


# 恢复销售数据
def restore_sold_data(backup_sold_data):
  goods_list = backup_sold_data
  return goods_list


# 修改备份数据
def update_backup_data(backup):
  good_name = input("输入需要修改销售记录的产品名：")
  for g in backup:
    if g.get("name") == good_name:
      quantity = int(input("商品数量："))
      price = float(input("单价："))
      g["quantity"] = quantity
      g["price"] = price
      print("备份已修改")
      break
    else:
      continue
  return


if __name__ == '__main__':
  goods_list = []
  backup = None
  while True:

    print("\n请选择操作：")
    print("1. 添加销售记录")
    print("2. 更新销售记录")
    print("3. 统计销售记录")
    print("4. 备份销售数据")
    print("5. 删除销售数据")
    print("6. 恢复销售数据")
    print("7. 修改备份数据（观察浅拷贝与深拷贝）")
    print("任意. 退出系统")

    choice = input("请输入数字执行程序：")
    if choice == "1":
      # 添加销售记录
      add_soled_data()
      print(goods_list)

    elif choice == "2":
      # 修改销售记录
      update_soled_data()
      print(goods_list)

    elif choice == "3":
      # 统计销售数据
      statistic_sold_sum()

    elif choice == "4":
      # 备份销售数据，备份后修改备份，换另一种方式再修改，再恢复
      t = input("请输入备份方式：shallow, deep")
      backup = back_up_sold_data(t, goods_list)

    elif choice == "5":
      # 删除销售记录
      delete_sold_data()
      print(goods_list)

    elif choice == "6":
      # 恢复销售数据
      goods_list = restore_sold_data(backup)
      print(goods_list)

      # 修改备份数据
    elif choice == "7":
      update_backup_data(backup)

    else:
      print("结束")
      exit()

      # 再调用统计销售数据查看区别 ----->  3
      # statistic_sold_sum()

      # 若copy的是浅拷贝：
      # 修改backup_sold_good会连带修改原数据，修改原数据也会修改备份数据backup_sold_good

      # 若copy的是深拷贝：
      # 修改backup_sold_good不会连带修改原数据，修改原数据也不会修改备份数据backup_sold_good






